package cn.tx.demo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class Jedistest {
//单实例连接
    /*public static void main(String[] args) {
       Jedis jedis= new Jedis("192.168.178.132",6379);

       jedis.set("name","bar");
       String name = jedis.get("name");
        System.out.println(name);
        jedis.close();
    }*/
/*使用连接池*/
    public static void main(String[] args) {
        JedisPoolConfig config = new JedisPoolConfig();
        //最大连接数
        config.setMaxTotal(60);
        //最大连接空闲数
        config.setMaxIdle(2);

        JedisPool pool = new JedisPool(config, "192.168.178.132", 6379);
        Jedis jedis = null;
        try  {
            jedis = pool.getResource();
            jedis.set("name", "lisi");
            String name = jedis.get("name");
            System.out.println(name);
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            if(jedis != null){
                //关闭连接
                jedis.close();
            }
        }
    }
}
